package net.yb.diary.service.impl;

import lombok.RequiredArgsConstructor;
import net.yb.diary.dto.LoginDTO;
import net.yb.diary.entity.User;
import net.yb.diary.exception.LoginErrorException;
import net.yb.diary.mapper.UserMapper;
import net.yb.diary.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Override
    public User login(LoginDTO loginDTO) {
        // 判断用户是否存在
        User user = userMapper.selectByUsername(loginDTO.getUsername());
        if (user == null){
            throw new LoginErrorException("用户不存在！");
        }

        // 判断密码是否正确
        if(!user.getPassword().equals(loginDTO.getPassword())){
            throw new LoginErrorException("密码错误！");
        }

        return user;
    }
}
